/************************************************************************

								Heroes IV
					  Copyright 2002, The 3DO Company

	------------------------------------------------------------------
  						     army_dialog_garrison.cpp

	$Header: $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "army_dialog_garrison.h"


#include "adv_artifact_pile.h"
#include "ownable_garrisonable_adv_object.h"


t_army_dialog_garrison::t_army_dialog_garrison( t_ownable_garrisonable_adv_object* garrison, t_army* adjacent_army,
												t_adventure_frame* adventure_frame,	
												t_creature_array * selected_army, int selected_slot )
	: t_army_dialog_base( garrison, adjacent_army, adventure_frame, selected_slot ),
	  m_garrison( garrison )
{
	create_buttons();
	create_creature_windows();
	create_icons();
	create_text_boxes();
	check_for_allied();

	// Crude hack to handle no-remove garrisons; out of time to do this right
	if ( !m_garrison->can_remove_garrison() )
	{
		m_army_window->set_allow_drags( false );

		if ( m_adjacent_army_window )
			m_adjacent_army_window->set_allow_drags( false );
	}

	// select first creature
	if (selected_slot < 0)
    {
		select_leader();
    }
	else
    {
        if ( m_creatures == selected_army )
    		m_army_window->select_creature( selected_slot );
        else
    	    m_adjacent_army_window->select_creature( selected_slot );
    }
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
void t_army_dialog_garrison::get_artifact_pile_position( t_adv_map_point & point, t_counted_ptr< t_adv_artifact_pile > & pile )
{
	// Try the garrison trigger cell
	t_adv_map_point trigger_point;

	if ( m_garrison->get_trigger_cell( trigger_point ) )
	{
		if ( t_adv_artifact_pile::is_good_creation_point( trigger_point, pile, m_garrison->get_map() ) )
		{
			point = trigger_point;
			return;
		}
	}
	else
	{
		assert( false ); // No trigger cell?
	}

	// Try adjacent army location
	if ( m_adjacent_army && m_adjacent_army->is_on_map() ) 
	{
		if ( t_adv_artifact_pile::is_good_creation_point( m_adjacent_army->get_position(), pile, m_garrison->get_map() ) )
		{
			point = m_adjacent_army->get_position();
			return;
		}
	}

	// Look around the current object
	if ( t_adv_artifact_pile::scan_for_good_creation_point( *m_garrison, *m_garrison, point, pile ) )
		return;

	// Nothing else for it...
	point = trigger_point;
}

// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
bool t_army_dialog_garrison::is_restricted( bool visiting_army ) const
{
	// Restrict no remove garrisons as if they were allied armies
	// Yes, it would be nice if more detailed restrictions could be
	// imposed; for instance, being able to move artifacts around the
	// in-garrison army but not move them out. However, it's too late 
	// to do that now. 
	if ( !visiting_army && !m_garrison->can_remove_garrison() )
		return true;

	return t_army_dialog_base::is_restricted( visiting_army );
}

